/*
 *  Copyright:  (C) 2018 LingYun IoT System Studio
 *              All rights reserved.
 *
 *  File name:  rlimit.c
 *Description:  Set limit of the number of connections.
 *
 *    Version:  1.1
 *     Author:  Guo Wenxue <guowenxue@gmail.com>
 *		        Copied by Zhu Tianyin <zhuty_cn@163.com>
 *  ChangeLog:  1.0  Release initial version in 2018.
 *              1.1  Copy and change some details on "01/12/2021".
 */
#include <stdio.h>  
#include <string.h>
#include <errno.h>
#include <sys/resource.h>  
  
void print_limits(char* name, int resource)
{ 
        struct rlimit limit;  
        if(getrlimit(resource, &limit) <0)
        {  
                printf("getrlimit failure: %s\n", strerror(errno));  
                return ;
        }  

        printf("%-15s ",name);  
        if(limit.rlim_cur == RLIM_INFINITY)
        {  
                printf("(infinite)     ");  
        }
        else
        {  
                printf("%-15ld",limit.rlim_cur);  
        } 

        if(limit.rlim_max == RLIM_INFINITY)
        {  
                printf("(infinite)     ");  
        }else
        {  
                printf("%-15ld",limit.rlim_max);  
        }  
        printf("\n");  
}  

int main(void)
{  
        struct rlimit limit = {0};

        print_limits("RLIMIT_NPROC", RLIMIT_NPROC);
        print_limits("RLIMIT_DATA", RLIMIT_DATA);
        print_limits("RLIMIT_STACK", RLIMIT_STACK);
        print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);

        printf("\nAfter set RLIMIT_NOFILE:\n");
        getrlimit(RLIMIT_NOFILE, &limit );
        limit.rlim_cur  = limit.rlim_max;
        setrlimit(RLIMIT_NOFILE, &limit );

        print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);

        return 0;
}
